﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.SystemManage;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-09-15 15:06
    /// 描 述：服务类
    /// </summary>
    public class StoreService :  RepositoryFactory
    {
        #region 获取数据
        public async Task<List<StoreEntity>> GetList(StoreListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<StoreEntity>> GetPageList(StoreListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list= await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<StoreEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<StoreEntity>(id);
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(StoreEntity entity)
        {
            AreaService areaService = new AreaService();
            //设置区域信息
            if (!entity._Area.IsEmpty())
            {
                var areaArr = TextHelper.SplitToArray<string>(entity._Area, ',');

                if (areaArr.Length == 1)
                {
                    var provinceData = areaService.GetEntityByAreaCode(areaArr[0]).Result;
                    entity.province_code = areaArr[0];
                    entity.province = provinceData.AreaName;
                    entity.city_code = "";
                    entity.district_code = "";
                }
                else if (areaArr.Length == 2)
                {
                    var provinceData = areaService.GetEntityByAreaCode(areaArr[0]).Result;
                    entity.province = provinceData.AreaName;
                    entity.province_code = areaArr[0];
                    var cityData = areaService.GetEntityByAreaCode(areaArr[1]).Result;
                    entity.city_code = cityData.AreaName;
                    entity.city_code = areaArr[1];
                    entity.district_code = "";
                }
                else
                {
                    var provinceData = areaService.GetEntityByAreaCode(areaArr[0]).Result;
                    entity.province = provinceData.AreaName;
                    var cityData = areaService.GetEntityByAreaCode(areaArr[1]).Result;
                    entity.city_code = cityData.AreaName;
                    var districtData = areaService.GetEntityByAreaCode(areaArr[2]).Result;
                    entity.district = districtData.AreaName;
                    entity.province_code = areaArr[0];
                    entity.city_code = areaArr[1];
                    entity.district_code = areaArr[2];
                }
            }
            else
            {
                entity.province_code = "";
                entity.city_code = "";
                entity.district_code = "";
                entity.province = "";
                entity.city = "";
                entity.district = "";
                entity.selected_area = "";
            }

            if (entity.Id.IsNullOrZero())
            {
                entity.Create();
                await this.BaseRepository().Insert(entity);
            }
            else
            {
                
                await this.BaseRepository().Update(entity);
            }

            var brandarr = TextHelper.SplitToArray<long>(entity.brandids, ',');

            var list = this.BaseRepository().IQueryable<StoreBrandRelation>(t => t.shopid == entity.Id);

            if (list.Count() > 0)
            {
                await this.BaseRepository().Delete<StoreBrandRelation>(list.Select(t => t.Id.Value).ToArray());
            }

            foreach (var item in brandarr)
            {
                StoreBrandRelation shopBrand = new StoreBrandRelation();

                shopBrand.brandid = item;

                shopBrand.Create();

                shopBrand.shopid = entity.Id;

                shopBrand.createtime = DateTime.Now;

                await this.BaseRepository().Insert(shopBrand);
            }

            //更新商品表，更新货物表
            //读取货物并更新货物
            var goods = this.BaseRepository().IQueryable<GoodsEntity>(t => t.shop_id == entity.Id).ToList();

            foreach (var item in goods)
            {
                item.shop_name = entity.shop_name;
                await this.BaseRepository().Update(item);
            }

            //读取货物并更新货物
            var hws = this.BaseRepository().IQueryable<GoodsWarehouseEntity>(t => t.shop_id == entity.Id).ToList();

            foreach (var item in hws)
            {
                item.shop_name = entity.shop_name;
                await this.BaseRepository().Update(item);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<StoreEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<StoreEntity, bool>> ListFilter(StoreListParam param)
        {
            var expression = LinqExtensions.True<StoreEntity>();
            if (param != null)
            {
            }
            return expression;
        }
        #endregion
    }
}
